<template>
  <view>
    <Header></Header>
    <view class="flex flex-center f36 fw pt3 pl3 flex-js">
      <view class="flex-center">
        <view class="g-c" @click="goIndex">测评</view>
        <view class="ml2 mr2">></view>
        <view class="">{{ type == 1 ? '同步评测' : '综合评测' }}</view>
      </view>
      <view class="flex">
        <block v-if="type == 1">
          <view style="width: 300rpx" class="mr3">
            <uni-data-select v-model="serachParams.grade_id" :localdata="listGrade" @change="change"></uni-data-select>
          </view>
          <view style="width: 300rpx" class="mr3">
            <uni-data-select v-model="serachParams.bookver_id" :localdata="listBookver"
              @change="change"></uni-data-select>
          </view>
        </block>
        <block v-else>
          <view style="width: 300rpx" class="mr3">
            <uni-data-select v-model="serachParams.stage_id" :localdata="listStage" @change="change"></uni-data-select>
          </view>
        </block>
      </view>
    </view>
    <view class="card" v-if="list.length != 0">
      <view class="list" v-for="(item, i) in list" :key="i">
        <view class="item">
          <view class="flex-js">
            <view class="flex-center" v-if="item.setsubject">
              <view style="position: relative;">
                <view class="text">{{ item.setsubject.name }}</view>
                <image class="avatar" src="/static/image/kmbg.jpg"></image>
              </view>
              <view class="fw ml2">{{ item.name }}</view>
            </view>
            <view class="flex">
              <view class="evaluating center mr1" v-if="item.rec_status == 1" @click="goResult(item)">查看结果</view>
              <view class="evaluating center" @click="goDetail(item)">{{ item.rec_status == 1 ? '重新评测' : '去评测' }}</view>
            </view>
          </view>
          <view class="flex mt3" v-if="type == 1">教材版本：{{ item.bookver_name }}
          </view>
          <view class="flex mt3">状态：{{ item.rec_status == 1 ? '已评测' : '待评测' }}
          </view>
          <view class="flex mt3">测评时间： {{ item.rec_time || '--' }}
          </view>
        </view>
      </view>
    </view>
    <view v-if="status == 'noMore'" class="noData f48 fw">暂无数据 </view>

  </view>
</template>

<script setup>
import { book, moduleclass, searchlist } from '/api'

const list = ref([])
const student_id = ref()
const type = ref()
const subject_ids = ref()

const listGrade = ref([])
const listBookver = ref([])
const listStage = ref([])
const status = ref('more')

const serachParams = reactive({
  grade_id: '',
  bookver_id: '',
  stage_id: '',
})

onLoad(async (options) => {
  student_id.value = options.student_id
  serachParams.grade_id = options.grade_id
  serachParams.stage_id = options.stage_id
  type.value = options.type
  const {
    data: {
      listgrade,
      listbookver,
      liststage,
    }
  } = await searchlist()
  listGrade.value = listgrade.map(i => ({ value: i.tag, text: i.name }));
  listBookver.value = listbookver.map(i => ({ value: i.value, text: i.label }));
  listStage.value = liststage.map(i => ({ value: i.stage_id, text: i.name }));

})
onShow(() => {
  init()
})

async function init() {
  status.value = 'loading'
  const params = { student_id: student_id.value, type: type.value, subject_ids: subject_ids.value }
  if (type.value == 2) {
    params.stage_id = serachParams.stage_id
  } else {
    params.grade_id = serachParams.grade_id
    params.bookver_id = serachParams.bookver_id
  }

  const {
    data: { data }
  } = type.value == 1 ? await book(params) : await moduleclass(params)

  list.value = data
  status.value = data.length === 0 ? 'noMore' : 'more'

}

function change(e) {
  init()
  console.log(e)
}

function goDetail(item) {
  uni.navigateTo({
    url: `/pages/evaluating/synchroTestingDetails?student_id=${student_id.value}&moduleclass_id=${type.value == 1 ? item.book_id : item.moduleclass_id}&type=${type.value}&title=${item.name}`
  })
}

function goResult(item) {
  uni.reLaunch({
    url: `/pages/evaluating/result?student_id=${student_id.value}&moduleclass_id=${type.value == 1 ? item.book_id : item.moduleclass_id}&type=${type.value}&title=${item.name}`
  })
}

function goIndex() {
  uni.navigateBack({
    delta: 1
  });
}
</script>

<style lang="scss" scoped>
.card {
  padding: 30rpx;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;

  .list {
    display: flex;
    flex-wrap: wrap;
    width: 33.33%;
    margin-bottom: 30rpx;
    max-width: 900rpx;
  }

  .item {
    width: 100%;
    // height: 300rpx;
    background-color: #f9f9f9;
    margin: 10rpx;
    border-radius: 8rpx;
    padding: 50rpx 30rpx;
    box-sizing: border-box;

    .avatar {
      width: 100rpx;
      height: 100rpx;
      border-radius: 50%;
    }

    .evaluating {
      width: 140rpx;
      height: 50rpx;
      background-color: #377CB7;
      color: #ffff;
      font-size: 24rpx;
      border-radius: 10rpx;
    }
  }
}

.text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  color: #000;
  font-weight: bold;
  font-size: 24rpx;
}
</style>
